package com.aerozhonghuan.archive.config;

import com.aerozhonghuan.archive.entity.CommonConstant;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@MapperScan("com.aerozhonghuan.archive.mapper")
public class DruidConfig {

    @Bean(name = CommonConstant.ARCHIVE)
    @ConfigurationProperties("spring.datasource.druid.archive")
    public DataSource archiveDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.BUSINESS)
    @ConfigurationProperties("spring.datasource.druid.business")
    public DataSource businessDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.USER)
    @ConfigurationProperties("spring.datasource.druid.user")
    public DataSource userDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.BSAP)
    @ConfigurationProperties("spring.datasource.druid.bsap")
    public DataSource bsapDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.CN6)
    @ConfigurationProperties("spring.datasource.druid.cn6")
    public DataSource cn6DataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.SIM)
    @ConfigurationProperties("spring.datasource.druid.sim")
    public DataSource simDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.ETC)
    @ConfigurationProperties("spring.datasource.druid.etc")
    public DataSource etcDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.FUELMANAGE)
    @ConfigurationProperties("spring.datasource.druid.fuelmanage")
    public DataSource fuelmanageDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.CLOUD)
    @ConfigurationProperties("spring.datasource.druid.cloud")
    public DataSource cloudDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.LOG)
    @ConfigurationProperties("spring.datasource.druid.log")
    public DataSource logDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.TSP)
    @ConfigurationProperties("spring.datasource.druid.tsp")
    public DataSource tspDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = CommonConstant.OTA)
    @ConfigurationProperties("spring.datasource.druid.ota")
    public DataSource otaDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public DynamicDataSource dynamicDataSource() {
        Map<Object, Object> targetDataSources = new HashMap<>(12);
        targetDataSources.put(CommonConstant.ARCHIVE, archiveDataSource());
        targetDataSources.put(CommonConstant.BUSINESS, businessDataSource());
        targetDataSources.put(CommonConstant.USER, userDataSource());
        targetDataSources.put(CommonConstant.BSAP, bsapDataSource());
        targetDataSources.put(CommonConstant.CN6, cn6DataSource());
        targetDataSources.put(CommonConstant.SIM, simDataSource());
        targetDataSources.put(CommonConstant.ETC, etcDataSource());
        targetDataSources.put(CommonConstant.FUELMANAGE, fuelmanageDataSource());
        targetDataSources.put(CommonConstant.CLOUD, cloudDataSource());
        targetDataSources.put(CommonConstant.LOG, logDataSource());
        targetDataSources.put(CommonConstant.TSP, tspDataSource());
        targetDataSources.put(CommonConstant.OTA, otaDataSource());


        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 设置默认数据源（此处默认使用 archive 数据源）
        dynamicDataSource.setDefaultTargetDataSource(archiveDataSource());
        // 设置所有目标数据源
        dynamicDataSource.setTargetDataSources(targetDataSources);

        // 保存原始数据源信息
        dynamicDataSource.setDefineTargetDataSources(targetDataSources);
        // 初始化 resolvedDataSources
        dynamicDataSource.afterPropertiesSet();

        return dynamicDataSource;
    }
}
